<?php

require "./utility.php";
require "./types.php";


$page = null;
$category = null;

if (!array_key_exists("page", $_GET)) {
    $page = 0;
} else {
    $page = $_GET['page'];
}
if (array_key_exists("category", $_GET)) {
    $category = $_GET["category"];
}

$conn = connect_mysql();

$where = $category ? "WHERE category=\"$category\"" : "";

$offset = (int)$page * 100;
$query_str = "SELECT * FROM Book $where LIMIT 100 offset $offset;";
$query = $conn->query($query_str);

$books = [];
while ($book_data = $query->fetch_row()) {
    $book = Book::fromArray($book_data);
    $books[] = $book;
}

$query->close();
$conn->close();

header("Content-Type: application/json");
echo json_encode($books, JSON_UNESCAPED_UNICODE);

?>